package com.personal.website.lottery.table

import java.util
import scala.collection.JavaConverters._

/**
 * Created by Gavin on 2015/12/14.
 */
class OddTable(issue: Int,redBalls: Seq[Int], blueBall: Int) extends Table(issue,redBalls,blueBall){
  val oddBalls = redBalls.filter(_ % 2 == 1)

  override def createRecord() = createOdds ++ createMainTable ++ createFrontSection ++
    createBackSection ++ createMissSection


  def createOddVsEven = s"${oddBalls.size}:${redBalls.size - oddBalls.size}"
  override def createSum(): Int = oddBalls.sum

  def createOdds = OddTable.ALL_EXISTS_BALLS.map(ball => if(oddBalls.contains(ball)) ball else DEFAULT_NON_EXISTS_VALUE)
  def createMainTable = (1 to 4).map(digit => if(oddBalls.count(ball => if(ball < 17 && ball % 8 == digit * 2 - 1 || ball > 17 && (ball -2) % 8 == digit * 2 - 1) true else false) > 0) digit else DEFAULT_NON_EXISTS_VALUE)
  def createFrontSection = (1 to 4).map(digit => if(oddBalls.count(ball => if(ball < 17 && ball % 8 == digit * 2 - 1) true else false) > 0) digit else DEFAULT_NON_EXISTS_VALUE)
  def createBackSection = (1 to 4).map(digit => if(oddBalls.count(ball => if(ball > 17 && (ball - 2) % 8 == digit * 2 - 1) true else false) > 0) digit else DEFAULT_NON_EXISTS_VALUE)
  def createMissSection = {
    (1 to 4).map { digit =>
      val actualDigit = if (digit <= 2) (digit - 1) * 8 + 1 else (digit - 1) * 8 + 3
      if(Range(actualDigit, actualDigit + 7, 2).count(oddBalls.contains(_)) == 0) digit else DEFAULT_NON_EXISTS_VALUE
    }
  }
  def createFrontSection1 = Seq(1,3).map(digit => if(createFrontSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 1).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createBackSection1 = Seq(1,3).map(digit => if(createBackSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 1).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createFrontSection2 = Seq(2,4).map(digit => if(createFrontSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 0).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createBackSection2 = Seq(2,4).map(digit => if(createBackSection.filter(x => x != DEFAULT_NON_EXISTS_VALUE && x % 2 == 0).contains(digit)) digit else DEFAULT_NON_EXISTS_VALUE)
  def createRearrangeBalls = OddTable.REARRANGE_BALLS.map(ball => if(oddBalls.contains(ball)) ball else DEFAULT_NON_EXISTS_VALUE)

  override def toString: String = Seq(Seq(issue),Seq(createOddVsEven),Seq(createSum()),createRecord()).flatten.mkString(";")
}

object OddTable{
  def apply(issue: Int,redBalls: Seq[Int], blueBall: Int) = new OddTable(issue,redBalls,blueBall)
  def apply(issue: Int,allBalls: Seq[Int]) = new OddTable(issue,allBalls.init,allBalls.last)
  def newInstance(issue: Integer,redBalls: util.List[Integer],blueBall: Integer) = new OddTable(issue.toInt, redBalls.asScala.map(_.toInt),blueBall.toInt)

  val ALL_EXISTS_BALLS = (1 to 33).filter(_ % 2 == 1)

  val REARRANGE_BALLS = Seq(1,9,5,13,19,27,23,31,3,11,7,15,21,29,25,33)

  val headString = Seq("issue","oddVsEven","sum",ALL_EXISTS_BALLS,"mainTable","frontSection","backSection","missSection","frontSection1","backSection1","frontSection2","backSection2","rearrangeBalls").mkString("|")

}
